package com.siwei.mes.sync.erp.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.config.UidUtils;
import com.siwei.mes.entity.dto.SyncLaboratoryDto;
import com.siwei.mes.entity.erp.*;
import com.siwei.mes.entity.experiment.ExperimentCheckItemorder;
import com.siwei.mes.entity.materials.Materials;
import com.siwei.mes.entity.mixproportion.MixProportion;
import com.siwei.mes.entity.mixproportion.MixProportionReport;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.enums.GenerateCodeEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.enums.SystemParamEnum;
import com.siwei.mes.mapper.erp.RwdextraExtendMapper;
import com.siwei.mes.mapper.erp.RwdextraUpdateLogMapper;
import com.siwei.mes.mapper.experiment.ExperimentCheckItemorderMapper;
import com.siwei.mes.mapper.mixProportion.MixProportionMapper;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.erp.RwdextraExtendService;
import com.siwei.mes.service.erp.RwdextraIronMarkService;
import com.siwei.mes.service.erp.RwdextraService;
import com.siwei.mes.service.laboratory.ContractInfoService;
import com.siwei.mes.service.laboratory.CustomInfoService;
import com.siwei.mes.service.laboratory.TaskListLaboratoryService;
import com.siwei.mes.service.materials.MaterialsService;
import com.siwei.mes.service.sync.erp.ErpCustomerService;
import com.siwei.mes.service.sync.erp.ErpProjectService;
import com.siwei.mes.service.system.SystemParamService;
import com.siwei.mes.sync.config.SyncProcessorService;
import com.siwei.mes.sync.erp.entity.GenConcreteResult;
import com.siwei.mes.sync.erp.entity.ItemorderFinish;
import com.siwei.mes.sync.erp.entity.NewTrwd;
import com.siwei.mes.sync.erp.entity.UpdateMix;
import com.siwei.mes.upload.erp.UploadErpService;
import com.siwei.mes.upload.erp.entity.ErpAddTaskPhbLog;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * ERP-任务单处理类
 *
 * @Description: ERP-任务单处理类
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/9/20 10:13
 */
@Service
public class SyncErpErpTrwdService {
    private static final Logger log = LoggerFactory.getLogger("erp");

    @Resource
    private RwdextraService rwdextraService;
    @Resource
    private RwdextraExtendService rwdextraExtendService;
    @Resource
    private GenConcreteService genConcreteService;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;
    @Resource
    private TaskListLaboratoryService taskListLaboratoryService;
    @Resource
    private RwdextraExtendMapper rwdextraExtendMapper;
    @Resource
    private ErpCustomerService erpCustomerService;
    @Resource
    private CustomInfoService customInfoService;
    @Resource
    private ErpProjectService erpProjectService;
    @Resource
    private ContractInfoService contractInfoService;
    @Resource
    private MaterialsService materialsService;
    @Resource
    private MixProportionMapper mixProportionMapper;
    @Resource
    private RwdextraIronMarkService rwdextraIronMarkService;
    @Resource
    private RwdextraUpdateLogMapper rwdextraUpdateLogMapper;
    @Resource
    private SyncProcessorService syncProcessorService;
    @Resource
    private ErpProjectMapper erpProjectMapper;
    @Resource
    private UploadErpService uploadErpService;
    @Resource
    private SystemParamService systemParamService;

    /**
     * erp新任务单
     *
     * @param newTrwd
     */
    public void newTrwd(NewTrwd newTrwd) {
        log.info("正在处理ERP通知新增任务单，入参：【{}】", JSON.toJSON(newTrwd));
        /**这里判断物料是否存在 只有C5-C100 和水下C20-水下C60  然后截取数据*/
        if (StringUtils.isNotBlank(newTrwd.getFTpz())) {
            materialsService.checkIsExist(MaterialsEnum.CONCRETE.getCode(), MaterialsEnum.CONCRETE.getName(), newTrwd.getFTpz());
        }

        Rwdextra rwdextra = rwdextraService.selectByFrwdh(newTrwd.getFRwdh());

        if (rwdextra == null) {
            log.info("根据任务单编号查询任务单信息失败，任务单编号：【{}】", newTrwd.getFRwdh());
            return;
        }

        RwdextraInfo rwdextraInfo = rwdextra.getRwdextraInfo();
        if (rwdextraInfo == null) {
            log.info("根据任务单编号查询任务单扩展信息失败，任务单编号：【{}】", newTrwd.getFRwdh());
            return;
        }
        String gcmc = rwdextraInfo.getFGcmc();//工程名称
        JSONObject proportionMaterial = null;
        if (StringUtils.isNotBlank(gcmc)) {
            /**这里处理铁标的逻辑  判断是否是铁标工程*/
            boolean isIronMark = false;
            ErpProject erpProject = erpProjectMapper.selectByName(gcmc);
            if (erpProject != null && erpProject.getIsIronMark() == 1) {
                isIronMark = true;
            }

            String phbNo = rwdextra.getPhbshow();
            Integer frwdh = rwdextra.getFrwdh();
            String fTpz = rwdextraInfo.getFTpz();

            log.info("ERP通知新增任务单，判断工程：【{}】是否是铁标工程：【{}】,对应配合比编号：【{}】", gcmc, isIronMark, phbNo);

            Materials materials = materialsService.checkIsExist(MaterialsEnum.CONCRETE.getCode(), MaterialsEnum.CONCRETE.getName(), fTpz);

            /**这里判断是否需要插入到铁标中间表去记录一下  只有是铁标才推送*/
            if (StringUtils.isBlank(phbNo) && isIronMark) {
                log.info("ERP通知新增任务单,该任务单配合比编号为空，查询对应物料是否有配合比编号，如果有就直接推送给ERP，任务单：【{}】", frwdh);
                /**判断对应物料是否绑定配合比  如果绑定了就不需要添加到关联表*/
                if (materials != null && StringUtils.isNotBlank(materials.getPhbNo())) {
                    log.info("ERP通知新增任务单,该任务单物料【{}】绑定了配合比【{}】，直接推送给ERP，任务单：【{}】", fTpz, materials.getPhbNo(), frwdh);

                    MixProportion selectByPhbNo = mixProportionMapper.selectByPhbNo(materials.getPhbNo());
                    if (selectByPhbNo != null) {
                        proportionMaterial = selectByPhbNo.getProportionMaterial();
                        /**推送到erp*/
                        try {
                            ErpAddTaskPhbLog erpAddTaskPhbLog = new ErpAddTaskPhbLog();
                            erpAddTaskPhbLog.setTaskIds(frwdh.toString());
                            erpAddTaskPhbLog.setPhbId(Integer.valueOf(selectByPhbNo.getErpPhbId()));
                            erpAddTaskPhbLog.setPhbNo(materials.getPhbNo());
                            uploadErpService.uploadAddTaskPhbLog(erpAddTaskPhbLog, false);
                        } catch (Exception e) {
                            log.info("ERP通知新增任务单,组装推送到erp失败，失败原因：{}", e.getMessage());
                        }
                    }
                } else {
                    /**新增铁标需求关联表*/
                    rwdextraIronMarkService.addInfo(rwdextra.getFrwdh(), gcmc, fTpz);
                }
            }
            /**如果配合比是对应列表中的也需要添加到关联表中*/
            List<String> notGenPhbNoList = genConcreteService.notGenPhbNoList();
            if (notGenPhbNoList.contains(phbNo) && isIronMark) {
                log.info("该任务单配合比编号为【{}】，需要添加到铁标需求关联表中，任务单：【{}】", JsonUtils.getJson(notGenPhbNoList), frwdh);
                /**新增铁标需求关联表*/
                rwdextraIronMarkService.addInfo(frwdh, gcmc, fTpz);

                if (materials != null && StringUtils.isNotBlank(materials.getPhbNo())) {
                    log.info("ERP通知新增任务单,该任务单物料【{}】绑定了配合比【{}】，直接推送给ERP，任务单：【{}】", fTpz, materials.getPhbNo(), frwdh);

                    MixProportion selectByPhbNo = mixProportionMapper.selectByPhbNo(materials.getPhbNo());
                    if (selectByPhbNo != null) {
                        /**推送到erp*/
                        try {
                            ErpAddTaskPhbLog erpAddTaskPhbLog = new ErpAddTaskPhbLog();
                            erpAddTaskPhbLog.setTaskIds(frwdh.toString());
                            erpAddTaskPhbLog.setPhbId(Integer.valueOf(selectByPhbNo.getErpPhbId()));
                            erpAddTaskPhbLog.setPhbNo(materials.getPhbNo());
                            uploadErpService.uploadAddTaskPhbLog(erpAddTaskPhbLog, false);
                        } catch (Exception e) {
                            log.info("ERP通知新增任务单,组装推送到erp失败，失败原因：{}", e.getMessage());
                        }
                    }
                }
            }
        }


        /**这里绑定上个任务单绑定原材料的信息*/
        RwdextraExtend lastMaterialsInfo = rwdextraExtendMapper.getLastMaterialsInfo();
        if (lastMaterialsInfo != null) {
            log.info("ERP通知新增任务单，开始绑定上一个任务单的原材料信息，上一条信息：【{}】", JsonUtils.getJson(lastMaterialsInfo));
            RwdextraExtend rwdextraExtend = rwdextra.getRwdextraExtend();
            if (rwdextraExtend != null) {
                JSONObject materialsInfoJson = lastMaterialsInfo.getMaterialsInfoJson();

                RwdextraExtendMaterials materialsInfo = JsonUtils.getObject(materialsInfoJson, RwdextraExtendMaterials.class);
                if (materialsInfo != null) {
                    materialsInfo.setFrwdh(rwdextra.getFrwdh());
                    rwdextraExtend.setUpdateTime(new Date());
                    setRwdextraExtendMaterials(materialsInfo, proportionMaterial);
                    rwdextraExtend.setMaterialsInfoJson(JsonUtils.beanToObject(materialsInfo));
                    rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);
                }
            }
        } else {
            log.info("没有上一个任务单绑定原材料的信息，不处理！");
        }

        /**这里操作协会的混凝土任务*/
        try {
            this.syncLaboratoryInfo(rwdextra, null, false, false, false);
        } catch (Exception e) {
            log.error("新任务单处理上传混凝土任务单协会软件发生错误，错误信息：{}", e.getMessage(), e);
        }
    }

    /**
     * ERP指派/更换配合比
     *
     * @param updateMix
     */
    public void updateMix(UpdateMix updateMix) {
        log.info("正在处理ERP通知修改任务单配合比，任务单号：【{}】，基准配比编号：【{}】", updateMix.getFRwdh(), updateMix.getFPhbNo());
        Rwdextra rwdextra = rwdextraService.selectByFrwdh(updateMix.getFRwdh());

        /**这里需要判断这个配合比编号是不是最后一次任务单修改记录的配合比编号*/
        RwdextraUpdateLog rwdextraUpdateLog = rwdextraUpdateLogMapper.selectByLastUpdateInfo(updateMix.getFRwdh());
        if (rwdextraUpdateLog != null) {
            try {
                log.info("ERP跟换配合比，判断配合比是否一致");
                JSONObject mixUpdateAfterJson = rwdextraUpdateLog.getMixUpdateAfterJson();
                MixProportion mixProportion = JsonUtils.getObject(mixUpdateAfterJson, MixProportion.class);
                String proportionPhb = mixProportion.getProportionPhb();
                if (!updateMix.getFPhbNo().equals(proportionPhb)) {
                    log.info("配合比不一致，需要清空对应试验委托与小票的关系，入参配合比：【{}】,最后一次配合比：【{}】", updateMix.getFPhbNo(), proportionPhb);
                    /**只要是这个任务单的相关小票 都要清除掉*/
                    List<ExperimentCheckItemorder> checkItemorderList = experimentCheckItemorderMapper.selectByFrwdh(updateMix.getFRwdh());
                    for (ExperimentCheckItemorder checkItemorder : checkItemorderList) {
                        experimentCheckItemorderMapper.deleteByPrimaryKey(checkItemorder.getId());
                    }
                }
            } catch (Exception e) {
                log.error("修改配合比，删除对应任务单的试验委托关系失败，任务单号：【{}】，配合比：【{}】", updateMix.getFPhbNo(), updateMix.getFPhbNo());
            }
        }

        log.info("给任务单打印json赋值【配合比】");
        rwdextraExtendService.setRwdextraPrintInfoPhb(rwdextra);
        /*李总确认不需要添加修改记录
        RwdextraInfo rwdextraInfo = rwdextra.getRwdextraInfo();
        if (rwdextraInfo != null) {
            *//**新增ERP任务单修改记录*//*
            rwdextraUpdateLogService.insertLog(updateMix.getFRwdh(), updateMix.getFPhbNo());
        }*/

        /**这里操作协会的混凝土任务*/
        String systemParamValue = systemParamService.selectBySystemCode(SystemParamEnum.PUSH_PRODUCE_HNT_SCWC, "0");
        log.info("系统参数：【{}】,开始生产后在推送任务单至协会,推送协会软件，推送状态：{}", SystemParamEnum.PUSH_PRODUCE_HNT_SCWC, systemParamValue);
        if (systemParamValue != null && !"1".equals(systemParamValue)) {
            try {
                SyncLaboratoryDto syncLaboratoryDto = new SyncLaboratoryDto().setRwdextra(rwdextra).setPhbNo(updateMix.getFPhbNo()).setIsMock(false).setIsNotify(false);
                syncProcessorService.submitRequest(syncLaboratoryDto);
            } catch (Exception e) {
                log.error("更换配合比处理上传混凝土任务单协会软件发生错误，错误信息：{}", e.getMessage(), e);
            }
        }

    }

    /**
     * ERP小票生产完成
     *
     * @param itemorderFinish
     */
    public void itemorderFinish(ItemorderFinish itemorderFinish) {
        log.info("正在处理ERP小票生产完成，任务单号：【{}】，小票号：【{}】，转料来源小票号：【{}】", itemorderFinish.getFRwdh(), itemorderFinish.getITID(), itemorderFinish.getFromItemoderNo());
        /**
         *  小票生产完成  才去走阈值逻辑 生成混凝土试验委托
         */
        GenConcreteResult genConcreteResult = genConcreteService.createConcreteExperiment(itemorderFinish);
        Boolean isExecFlag = genConcreteResult.getIsExecFlag();
        /**只有等这里的逻辑处理完了 再去处理之后的*/
        if (isExecFlag) {
            Rwdextra rwdextra = genConcreteResult.getRwdextra();
            if (rwdextra == null) {
                log.info("处理ERP小票生产完成，根据任务单编号查询任务单扩展信息失败，任务单编号：【{}】", itemorderFinish.getFRwdh());
                return;
            }
            Integer finishCarNumber = itemorderFinish.getFinishCarNumber();//累计车数


            RwdextraPrintEntity rwdextraPrintInfo = new RwdextraPrintEntity().setRwdextra(rwdextra).setGenConcreteResult(genConcreteResult).setItemorderFinish(itemorderFinish).setIsUpdate(false);

            if (finishCarNumber != null && finishCarNumber == 1) {
                String qualityCertificateNo = UidUtils.genNoNumber(GenerateCodeEnum.QUALITY_CERTIFICATE.getCode(), "%05d");
                log.info("小票第一车生产完成，任务单号：【{}】，小票号：【{}】,质量证明书编号：【{}】", itemorderFinish.getFRwdh(), itemorderFinish.getITID(), qualityCertificateNo);
                rwdextraExtendService.hasItemorder(rwdextra, qualityCertificateNo);

                //第一车已完成 才调用
                //任务单同步到协会软件   新增或修改操作到协会
                try {
                    this.syncLaboratoryInfo(rwdextra, null, false, false, true);
                } catch (Exception e) {
                    log.error("ERP小票生产完成处理上传混凝土任务单协会软件发生错误，错误信息：{}", e.getMessage(), e);
                }
                rwdextraPrintInfo.setIsFirstCar("1");
            } else {
                log.info("小票生产完成，任务单号：【{}】，小票号：【{}】", itemorderFinish.getFRwdh(), itemorderFinish.getITID());
                if (rwdextra.getRwdextraExtend() != null) {
                    rwdextraExtendService.updateSupplyEndTime(rwdextra.getRwdextraExtend());
                }
                rwdextraPrintInfo.setIsFirstCar("0");
            }
            syncProcessorService.submitRequest(rwdextraPrintInfo);
        }
    }


    /**
     * 处理上传混凝土任务单协会软件中
     *
     * @param rwdextra          erp任务单
     * @param phbNo             跟换配比的配比编号
     * @param isNotify          true-报错  false-不报错
     * @param isMock            true-模拟同步  false-不是模拟同步
     * @param isItemorderFinish true-小票生产完成调用
     */
    public void syncLaboratoryInfo(Rwdextra rwdextra, String phbNo, boolean isNotify, boolean isMock,
                                   boolean isItemorderFinish) {
        if (rwdextra == null) {
            log.info("正在处理操作协会相关数据，任务单信息为空，不处理");
            return;
        }
        log.info("正在处理操作协会相关数据，对应ERP任务单号：【{}】", rwdextra.getFrwdh());

        RwdextraInfo rwdextraInfo = rwdextra.getRwdextraInfo();//ERP任务单扩展信息
        RwdextraExtend rwdextraExtend = rwdextra.getRwdextraExtend();//品控任务单扩展信息

        if (rwdextraInfo != null) {
            String fHtdw = rwdextraInfo.getFHtdw();//合同单位

            /**新增客户信息到品控*/
            ErpCustomer erpCustomer = erpCustomerService.saveOrUpdateByErp(fHtdw);
            rwdextraExtend.setErpCustomerId(erpCustomer.getId()).setUpdateTime(new Date());
            rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);

            /**上传客户信息到协会*/
            try {
                customInfoService.saveOrUpdate(erpCustomer, true, isMock);
            } catch (Exception e) {
                log.error("ERP新任务单对应客户信息上传到协会失败，错误原因：【{}】", e.getMessage(), e);
                assertTrue(isNotify, "ERP新任务单对应客户信息上传到协会失败");
            }

            /**新增工程信息到品控*/
            ErpProject erpProject = erpProjectService.saveOrUpdateErp(erpCustomer, rwdextraInfo);
            rwdextraExtend.setErpProjectId(erpProject.getId()).setUpdateTime(new Date());
            rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);
            /**上传工程和合同到协会*/
            try {
                contractInfoService.saveOrUpdate(erpProject, true, isMock);
            } catch (Exception e) {
                log.error("ERP新任务单对应工程与合同信息上传到协会失败，错误原因：【{}】", e.getMessage(), e);
                assertTrue(isNotify, "ERP新任务单对应工程与合同信息上传到协会失败");
            }
        }

        //如果为小票生产完成直接上传  否则根据参数判断是否上传
        if (isItemorderFinish) {
            /**新增任务单并上传到协会   新增操作到协会*/
            try {
                taskListLaboratoryService.insertProduceHnt(rwdextra, phbNo, isMock);
            } catch (Exception e) {
                log.error("上传协会任务单失败：" + e.getMessage(), e);
                //任务单扩展表   记录失败原因
                rwdextraExtend.setReason(e.getMessage()).setStatus("0").setUpdateTime(new Date());
                rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);
                assertTrue(isNotify, "上传协会任务单失败：" + e.getMessage());
            }
        } else {
            String systemParamValue = systemParamService.selectBySystemCode(SystemParamEnum.PUSH_PRODUCE_HNT_SCWC, "0");
            log.info("系统参数：【{}】,开始生产后在推送任务单至协会,推送协会软件，推送状态：{}", SystemParamEnum.PUSH_PRODUCE_HNT_SCWC, systemParamValue);
            if (systemParamValue != null && !"1".equals(systemParamValue)) {
                /**新增任务单并上传到协会   新增操作到协会*/
                try {
                    taskListLaboratoryService.insertProduceHnt(rwdextra, phbNo, isMock);
                } catch (Exception e) {
                    log.error("上传协会任务单失败：" + e.getMessage(), e);
                    //任务单扩展表   记录失败原因
                    rwdextraExtend.setReason(e.getMessage()).setStatus("0").setUpdateTime(new Date());
                    rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);
                    assertTrue(isNotify, "上传协会任务单失败：" + e.getMessage());
                }
            }
        }

    }

    /**
     * 处理上传混凝土任务单协会软件中
     *
     * @param syncLaboratoryDto 上传混凝土任务单协会软件dto
     */
    public void syncLaboratoryInfoDto(SyncLaboratoryDto syncLaboratoryDto) {
        Rwdextra rwdextra = syncLaboratoryDto.getRwdextra();
        String phbNo = syncLaboratoryDto.getPhbNo();
        boolean isMock = syncLaboratoryDto.getIsMock();
        boolean isNotify = syncLaboratoryDto.getIsNotify();
        log.info("正在处理操作协会相关数据，对应ERP任务单号：【{}】", rwdextra.getFrwdh());

        RwdextraInfo rwdextraInfo = rwdextra.getRwdextraInfo();//ERP任务单扩展信息
        RwdextraExtend rwdextraExtend = rwdextra.getRwdextraExtend();//品控任务单扩展信息

        if (rwdextraInfo != null) {
            String fHtdw = rwdextraInfo.getFHtdw();//合同单位

            /**新增客户信息到品控*/
            ErpCustomer erpCustomer = erpCustomerService.saveOrUpdateByErp(fHtdw);
            rwdextraExtend.setErpCustomerId(erpCustomer.getId()).setUpdateTime(new Date());
            rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);

            /**上传客户信息到协会*/
            try {
                customInfoService.saveOrUpdate(erpCustomer, true, isMock);
            } catch (Exception e) {
                log.error("ERP新任务单对应客户信息上传到协会失败，错误原因：【{}】", e.getMessage(), e);
                assertTrue(isNotify, "ERP新任务单对应客户信息上传到协会失败");
            }

            /**新增工程信息到品控*/
            ErpProject erpProject = erpProjectService.saveOrUpdateErp(erpCustomer, rwdextraInfo);
            rwdextraExtend.setErpProjectId(erpProject.getId()).setUpdateTime(new Date());
            rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);
            /**上传工程和合同到协会*/
            try {
                contractInfoService.saveOrUpdate(erpProject, true, isMock);
            } catch (Exception e) {
                log.error("ERP新任务单对应工程与合同信息上传到协会失败，错误原因：【{}】", e.getMessage(), e);
                assertTrue(isNotify, "ERP新任务单对应工程与合同信息上传到协会失败");
            }
        }

        /**新增任务单并上传到协会   新增操作到协会*/
        try {
            taskListLaboratoryService.insertProduceHnt(rwdextra, phbNo, isMock);
        } catch (Exception e) {
            log.error("上传协会任务单失败：" + e.getMessage(), e);
            //任务单扩展表   记录失败原因
            rwdextraExtend.setReason(e.getMessage()).setStatus("0").setUpdateTime(new Date());
            rwdextraExtendMapper.updateByPrimaryKeySelective(rwdextraExtend);
            assertTrue(isNotify, "上传协会任务单失败：" + e.getMessage());
        }
    }

    /**
     * 获取配合比信息中的厂家 厂家简称 供应商 供应商简称 物料类型 物料名称 物料规格 材料类型 材料名称 材料规格 赋值到对应的原材料信息中
     *
     * @param materialsInfo      任务单扩展记录-所有原材料
     * @param proportionMaterial 配合比原材料json
     * @return
     */
    private void setRwdextraExtendMaterials(RwdextraExtendMaterials materialsInfo, JSONObject proportionMaterial) {
        MixProportionReport mixProportionReport = JsonUtils.getObject(proportionMaterial, MixProportionReport.class);
        if (mixProportionReport == null) {
            return;
        }
        MixProportionReport.BgxxInfo sn = mixProportionReport.getSn();//水泥
        RwdextraExtendMaterialsInfo snMaterials = materialsInfo.getSnInfo();
        materialsInfo.setSnInfo(this.payloadVerifyRecordInfo(sn, snMaterials));

        MixProportionReport.BgxxInfo fmh = mixProportionReport.getFmh();//粉煤灰
        RwdextraExtendMaterialsInfo fmhMaterials = materialsInfo.getFmhInfo();
        materialsInfo.setFmhInfo(this.payloadVerifyRecordInfo(fmh, fmhMaterials));

        MixProportionReport.BgxxInfo kzf = mixProportionReport.getKzf();//矿渣粉
        RwdextraExtendMaterialsInfo kzfMaterials = materialsInfo.getSnInfo();
        materialsInfo.setKzfInfo(this.payloadVerifyRecordInfo(kzf, kzfMaterials));

        MixProportionReport.BgxxInfo cgl = mixProportionReport.getCgl();//粗骨料
        RwdextraExtendMaterialsInfo cglMaterials = materialsInfo.getSnInfo();
        materialsInfo.setCglInfo(this.payloadVerifyRecordInfo(cgl, cglMaterials));

        MixProportionReport.BgxxInfo xgl = mixProportionReport.getXgl();//细骨料
        RwdextraExtendMaterialsInfo xglMaterials = materialsInfo.getSnInfo();
        materialsInfo.setXglInfo(this.payloadVerifyRecordInfo(xgl, xglMaterials));

        MixProportionReport.BgxxInfo wjj1 = mixProportionReport.getWjj1();//外加剂1
        RwdextraExtendMaterialsInfo wjjMaterials = materialsInfo.getWjjInfo();
        materialsInfo.setWjjInfo(this.payloadVerifyRecordInfo(wjj1, wjjMaterials));
    }

    /**
     * 1.任务单的工程名称 和配合比编号  去查询验证记录有没有
     *
     * @param bgxxInfo  配合比中的原材料信息
     * @param materials 任务单计划时间
     * @return
     */
    private RwdextraExtendMaterialsInfo payloadVerifyRecordInfo(MixProportionReport.BgxxInfo bgxxInfo, RwdextraExtendMaterialsInfo materials) {
        String clmc = bgxxInfo.getClmc();//材料名称
        String clgg = bgxxInfo.getClgg();//材料规格
        if (StringUtils.isBlank(clmc) || StringUtils.isBlank(clgg)) {
            return materials;
        }
        materials.setMaterialName(clmc);
        materials.setMaterialSpec(clgg);
        materials.setFactory(bgxxInfo.getCj());
        materials.setFactoryCalled(bgxxInfo.getCjjc());
        materials.setSupplyCompanyCalled(bgxxInfo.getGys());
        materials.setSupplyCompanyCalled(bgxxInfo.getGysjc());
        return materials;
    }
}
